<!DOCTYPE html>
<html>
	<<!-- 作者：offline 时间：2018-06-27 描述：部门查询 -->

		<head>
			<!-- 引入框架文件 -->
			<link rel="stylesheet" href="../../model/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
			<script type="text/javascript" src="../../model/jquery-2.1.0.js"></script>
			<script type="text/javascript" src="../../model/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
			<link rel="stylesheet" href="../../static/css/ehr.css" />
			<link rel="stylesheet" href="../../static/css/employee.css" />
			<script type="text/javascript" src="../../model/models.js"></script>
			<script type="text/javascript" src="../../model/dispose.js"></script>
			<style type="text/css">
				.show th {
					text-align: center;
				}
				
				.show td {
					text-align: center;
				}
			</style>
			<meta charset="UTF-8">
			<title></title>
		</head>

		<body>

			<div include="../../static/header.htm"></div>
			<div style="height: 100%;float: left;" include="../../static/aside.htm"></div>

			<div class="aside2">
				<div include="../../static/coManage.htm"></div>
			</div>

			<div class="section">
				<div include="../../static/coDivisManage.htm"></div>
				<div class="content">
					<!-- 部门搜索框 -->
					<form id="findDepa">
						<div class="">
							<input id="key" class="form-control" style="width: 250px; float: left;margin-right: 10px;" placeholder="部门编号/名称" type="text" />
							<input class="btn btn-primary" type="submit" value="查询" />
						</div>
					</form>
					<!-- 显示部门信息 -->
					<div class="show">
						<table class="table table-striped">
							<thead>
								<tr>
									<th>部门编号</th>
									<th>部门名称</th>
									<th>部门类型</th>
									<th>部门电话</th>
									<th>部门传真</th>
									<th>描述</th>
									<th>上级部门</th>
									<th>成立日期</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="depaTbody">

							</tbody>
						</table>
						<!-- 分页开始 -->
						<nav style="position: absolute; bottom: 20px;" aria-label="Page navigation">
							<ul id="pagination" class="pagination">
								<li>
									<a href="#" aria-label="Previous">
										<span aria-hidden="true">&laquo;</span>
									</a>
								</li>
								<li>
									<a href="#" aria-label="Next">
										<span aria-hidden="true">&raquo;</span>
									</a>
								</li>
							</ul>
						</nav>

						<!-- 分页结束 -->

					</div>
				</div>
			</div>
		</body>

</html>

<script>
	/**
	 * 根据关键字查信息
	 * @param {Object} key 关键字
	 * @param {Object} page 当前页
	 */
	function findByKey(key, page) {
		$.ajax({
			type: "post",
			url: "http://localhost:9090/findDepaByKey?key=" + key + "&page=" + page,
			"success": function(obj) {
				//debugger; 断点
				//debugger;
				if(obj.data != null) {

					var $tbody = $("#depaTbody");
					$tbody.html("");

					var depas = obj.data;
					// 分页页面初始化
					upPage("pagination", obj.page);

					if(null == depas.length) {
						var depa = depas;

						var $tr = $("<tr id='" + depas.id + "'></tr>");

						for(var index in depa) {
							if("serialNum" != index) {
								var $td = $("<td>" + depa[index] + "</td>")

								$tr.append($td);
							}
						}
						$tr.append("<td><a href='#' onclick='delDepa(" + depas.id + ")'>删除</a></td>")

						$tbody.append($tr);
					} else {
						for(var i = 0; i < depas.length; i++) {
							var depa = depas[i];

							var $tr = $("<tr id='" + depas[i].id + "'></tr>");

							for(var index in depa) {
								if("serialNum" != index) {
									if("depaHigher" == index) {
										if(depa[index] != null) {
											var $td = $("<td>" + depa[index].depaName + "</td>")
										} else {
											var $td = $("<td>无</td>")
										}

									} else {
										var $td = $("<td>" + depa[index] + "</td>")
									}
									$tr.append($td);
								}
							}
							$tr.append("<td><a href='#' onclick='delDepa(" + depas[i].id + ")'>删除</a></td>")

							$tbody.append($tr);
						}
					}

				}
			},
			"dataType": "json",
			async: true
		});
	}

	$("#findDepa").submit(function(e) {
		e.preventDefault();

		var key = $("#key").val();

		findByKey(key, 1);

	})

	function delDepa(id) {

		var txt = "确认删除编号为：" + id + "  的岗位";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.warning, {
			onOk: function() {
				window.wxc.xcConfirm("是否删除该部门旗下的所有部门！", window.wxc.xcConfirm.typeEnum.warning, {
					onOk: function() {
						$.ajax({
							type: "post",
							url: "http://localhost:9090/delDepa?id=" + id + "&flag=" + true,
							"success": function(obj) {
								//debugger; 断点
								//debugger;
								if(obj.data) {
									window.wxc.xcConfirm("删除成功！", "success");
									$("#" + id).html("");
								} else {
									window.wxc.xcConfirm("删除失败！", "error");
								}
							},
							"dataType": "json",
							async: true
						});
						return true;
					},
					onCancel: function() {
						$.ajax({
							type: "post",
							url: "http://localhost:9090/delDepa?id=" + id + "&flag=" + false,
							"success": function(obj) {
								//debugger; 断点
								//debugger;
								if(obj.data) {
									window.wxc.xcConfirm("删除成功！", "success");
									$("#" + id).html("");
								} else {
									window.wxc.xcConfirm("删除失败！", "error");
								}
							},
							"dataType": "json",
							async: true
						});
						return true;
					}
				});
			},
		});
	};
</script>